/**
 * Created by wujun on 2017/4/27.
 */
//利用匿名函数将脚本包裹起来
(function(){
  var dateutil = {};

  dateutil.Format = function(date, fmt) { //author: meizz
    var o = {
      "M+" : date.getMonth()+1,                 //月份
      "d+" : date.getDate(),                    //日
      "h+" : date.getHours(),                   //小时
      "m+" : date.getMinutes(),                 //分
      "s+" : date.getSeconds(),                 //秒
      "S"  : date.getMilliseconds(),             //毫秒
      "TT" : date.getHours() < 12 ? "AM" : "PM",
    };
    if (/(y+)/.test(fmt)){
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "")
        .substr(4 - RegExp.$1.length));
    }
    for ( var k in o){
      if (new RegExp("(" + k + ")").test(fmt)){
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
          : (("00" + o[k]).substr(("" + o[k]).length)));
      };
    }
    return fmt;
  };


  // 时间戳 转字符串  按【2012-02-13 09:09:09】的格式返回日期
  dateutil.timestamp2Time   =function   (val) {
    console.log("timestamp2Time.val:"+val);
    if(!val)return val;
    var rtnstr=val;
    try {
      if (val.indexOf('-') >= 0) {
        return val;
      }
      else {
        var d = new Date();
        var re = /-?\d+/;
        var m = re.exec(val);
        var d = new Date(parseInt(m[0]));
        rtnstr = dateutil.Format (d,"yyyy-MM-dd hh:mm:ss");
      }
    } catch (e) {
      console.log("timestamp2Time.error:"+e);
      rtnstr = "";
    }
    console.log("timestamp2Time.rtnstr:"+rtnstr);

    return rtnstr;
  }

  //时间戳 转字符串  按【2012-02-13】的格式返回日期
  dateutil.timestamp2Date =function(val) {
    try {
      if (val.indexOf('-') >= 0) {
        return val;
      }
      else {
        var d = new Date();
        var re = /-?\d+/;
        var m = re.exec(val);
        var d = new Date(parseInt(m[0]));
        return dateutil.Format(d,"yyyy-MM-dd");
      }
    } catch (e) {
      return "";
    }
  }



///////////add your function here///////////////////////////


////////////////////////////////////////////////////////////
//定义全局变量命令空间
  window.DateUtil = dateutil;
////////////////////////////////////////////////////////////
})();
